{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Distribution Test Tables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example demonstrates how to create some conditional probability tables and a bayesian network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pomegranate import *\n",
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First let's define some conditional probability tables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "c_table = [[0, 0, 0, 0.6],\n",
    "           [0, 0, 1, 0.4],\n",
    "           [0, 1, 0, 0.7],\n",
    "           [0, 1, 1, 0.3],\n",
    "           [1, 0, 0, 0.2],\n",
    "           [1, 0, 1, 0.8],\n",
    "           [1, 1, 0, 0.9],\n",
    "           [1, 1, 1, 0.1]]\n",
    "\n",
    "d_table = [[ 0, 0, 0.5 ],\n",
    "           [ 0, 1, 0.5 ],\n",
    "           [ 1, 0, 0.3 ],\n",
    "           [ 1, 1, 0.7 ]]\n",
    "\n",
    "f_table = [[ 0, 0, 0, 0.8 ],\n",
    "           [ 0, 0, 1, 0.2 ],\n",
    "           [ 0, 1, 0, 0.3 ],\n",
    "           [ 0, 1, 1, 0.7 ],\n",
    "           [ 1, 0, 0, 0.6 ],\n",
    "           [ 1, 0, 1, 0.4 ],\n",
    "           [ 1, 1, 0, 0.9 ],\n",
    "           [ 1, 1, 1, 0.1 ]]\n",
    "\n",
    "e_table = [[ 0, 0, 0.7 ],\n",
    "           [ 0, 1, 0.3 ],\n",
    "           [ 1, 0, 0.2 ],\n",
    "           [ 1, 1, 0.8 ]]\n",
    "\n",
    "g_table = [[ 0, 0, 0, 0.34 ],\n",
    "           [ 0, 0, 1, 0.66 ],\n",
    "           [ 0, 1, 0, 0.83 ],\n",
    "           [ 0, 1, 1, 0.17 ],\n",
    "           [ 1, 0, 0, 0.77 ],\n",
    "           [ 1, 0, 1, 0.23 ],\n",
    "           [ 1, 1, 0, 0.12 ],\n",
    "           [ 1, 1, 1, 0.88 ]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then let's convert them into distribution objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = DiscreteDistribution({ 0: 0.5, 1: 0.5 })\n",
    "b = DiscreteDistribution({ 0: 0.7, 1: 0.3 })\n",
    "e = ConditionalProbabilityTable( e_table, [b] )\n",
    "c = ConditionalProbabilityTable( c_table, [a,b] )\n",
    "d = ConditionalProbabilityTable( d_table, [c] )\n",
    "f = ConditionalProbabilityTable( f_table, [c,e] )\n",
    "g = ConditionalProbabilityTable( g_table, [c,e] )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we can convert these distributions into states."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "a_s = State( a, \"a\" )\n",
    "b_s = State( b, \"b\" )\n",
    "c_s = State( c, \"c\" )\n",
    "d_s = State( d, \"d\" )\n",
    "e_s = State( e, \"e\" )\n",
    "f_s = State( f, \"f\" )\n",
    "g_s = State( g, \"g\" )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we have our states created, we can finally start making our bayesian network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = BayesianNetwork( \"derp\" )\n",
    "model.add_nodes( a_s, b_s, c_s, d_s, e_s, f_s, g_s )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we define the edges."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add_edge( a_s, c_s )\n",
    "model.add_edge( b_s, c_s )\n",
    "model.add_edge( c_s, d_s )\n",
    "model.add_edge( c_s, f_s )\n",
    "model.add_edge( b_s, e_s )\n",
    "model.add_edge( e_s, f_s )\n",
    "model.add_edge( c_s, g_s )\n",
    "model.add_edge( e_s, g_s )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We finish by baking the network to finalize its structure."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.bake()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can check on the structure of our bayesian network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a          : {0: 0.5, 1: 0.5}\n",
      "b          : {0: 0.6999999999999997, 1: 0.3000000000000003}\n",
      "c          : {0: 0.52, 1: 0.4799999999999999}\n",
      "d          : {0: 0.4040000000000001, 1: 0.596}\n",
      "e          : {0: 0.5499999999999998, 1: 0.45000000000000034}\n",
      "f          : {0: 0.6518, 1: 0.34820000000000007}\n",
      "g          : {0: 0.52066, 1: 0.4793399999999999}\n"
     ]
    }
   ],
   "source": [
    "print(\"\\n\".join( \"{:10.10} : {}\".format( state.name, belief.parameters[0] ) for state, belief in zip( model.states, model.predict_proba({}, max_iterations=100) ) ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
